package com.jr.Dao.Impl;

import com.jr.Dao.IUsersDao;
import com.jr.Pojo.Package_updates;
import com.jr.Pojo.Packages;
import com.jr.Pojo.Users;
import com.jr.util.DBHelper;
import com.jr.util.PageHelper;
import com.sun.org.apache.xpath.internal.objects.XObject;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

public class UsersDaoImpl implements IUsersDao {
    DBHelper db = new DBHelper();
    @Override
    public int selectAllDataCount(String id,String receiver) {
        StringBuilder sql=new StringBuilder("select count(*) from packages where receiver='").append(receiver).append("'");
        if(!id.equals("null")){
            sql.append(" and id='").append(id).append("'");
        }
        Connection con=null;
        PreparedStatement ps=null;
        ResultSet resultSet=null;
        try {
            con=db.getCon();
            ps=con.prepareStatement(sql.toString());
            resultSet=ps.executeQuery();
            if(resultSet.next()){
                return resultSet.getInt(1);
            }
        } catch (SQLException e) {
            e.printStackTrace();
        }finally {
            db.closeAll(con,ps,resultSet);
        }
        return 0;
    }

    @Override
    public ArrayList<Packages> selectAll(String id,String receiver,PageHelper ph) {
        StringBuilder sql=new StringBuilder("select * from packages where receiver='").append(receiver).append("'");
        if(!id.equals("null")){
            sql.append(" and id='").append(id).append("'");
        }
        sql.append(" limit ?,?");
        int index=(ph.getPageIndex()-1)*ph.getShowNumber();

        return db.dtQuery(sql.toString(),Packages.class,index,ph.getShowNumber());
    }
}
